package com.storeroompda.ui.enterstore;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;
import com.storeroompda.R;
import com.storeroompda.base.views.BaseActivity;
import com.storeroompda.bean.ItemBean;
import com.storeroompda.controlview.EditTextWithSearchButton;
import com.storeroompda.dialogs.ChooseDialog;
import com.storeroompda.dialogs.PopDialog;
import com.storeroompda.entity.BillCodeInfo;
import com.storeroompda.tool.TimeControl;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class EnterStoreActivity extends BaseActivity<EnterStoreModel, EnterStoreView, EnterStorePresent> implements EnterStoreView {
    private String tag = "EnterStoreActivity";
    @BindView(R.id.et1)
    EditText et1;
    @BindView(R.id.bt1)
    Button bt1;
    @BindView(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_r)
    TextView tvR;
    @BindView(R.id.tvc1)
    TextView tvc1;
    @BindView(R.id.rl1)
    RelativeLayout rl1;
    @BindView(R.id.tvc2)
    TextView tvc2;
    @BindView(R.id.rl2)
    RelativeLayout rl2;
    @BindView(R.id.nbt)
    Button nbt;
    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.sws)
    Switch sws;
    @BindView(R.id.tvs)
    TextView tvs;
    List<BillCodeInfo> list = new ArrayList<BillCodeInfo>();
    ProductAdapter pa;
    ItemAdapter ia;
    boolean deleteFlag = false;
    List<ItemBean> beanList;
    private String idd = "";
    private String name = "";
    private BillCodeInfo billCodeInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_enterstore);
        ButterKnife.bind(this);
        tvTitle.setText("生产入库");
        tvc2.setText(TimeControl.getdate());
        initBroadcastReciever();
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0) {
                    return;
                }
                for (int i = 0; i < list.size(); i++) {
                    list.get(i).setFlag(false);
                }
                list.get(position - 1).setFlag(true);
                billCodeInfo = list.get(position - 1);
                pa.notifyDataSetChanged();
            }
        });

        listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                final int pos=position;
                if (pos == 0) {
                    return true;
                }
                ChooseDialog nDialog = new ChooseDialog(EnterStoreActivity.this, R.style.Dialog1, new ChooseDialog.PriorityListener() {
                    @Override
                    public void setActivityText() {
                        presenter.deleteBill(list.get(pos - 1));
                    }
                },"删除单据");
                WindowManager.LayoutParams lp = nDialog.getWindow().getAttributes();
                lp.gravity = Gravity.CENTER;
                nDialog.show();
                return true;
            }
        });

        View headView = getLayoutInflater().inflate(R.layout.list_head, null);
        listview.addHeaderView(headView);
        presenter.showplist();
        presenter.showBill();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public EnterStoreModel createModel() {
        return new EnterStoreModelImpl(this);
    }

    @Override
    public EnterStoreView createView() {
        return this;
    }

    @Override
    public EnterStorePresent createPresenter() {
        return new EnterStorePresent();
    }

    @OnClick({R.id.iv_title_left, R.id.nbt, R.id.sws, R.id.rl1, R.id.rl2,R.id.bt1})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.nbt:
                if (idd.equals("")) {
                    tvs.setText("请选择产品");
                    return;
                }
                BillCodeInfo billCodeInfo1 = new BillCodeInfo(null, tvc2.getText().toString(), idd, name, 0, 1);
                presenter.addBill(billCodeInfo1);
                break;
            case R.id.sws:
                deleteFlag = !deleteFlag;
                break;
            case R.id.rl1:
                showPop(view);
                break;
            case R.id.rl2:
                chooseDate(tvc2);
                break;
            case R.id.bt1:
                if (deleteFlag)//删除条码
                {
                    presenter.delCode(billCodeInfo,et1.getText().toString());
                } else //增加条码
                {
                    presenter.addCode(billCodeInfo,et1.getText().toString());
                }
                break;
        }
    }

    private void showPopupWindow(View view) {
        View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_data, null);
        final PopupWindow popupWindow = new PopupWindow(contentView,
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setTouchable(true);
        ListView listview = (ListView) contentView.findViewById(R.id.listview);
        EditTextWithSearchButton es = (EditTextWithSearchButton) contentView.findViewById(R.id.etid);

        ia = new ItemAdapter(this, beanList);
        listview.setAdapter(ia);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                tvc1.setText(beanList.get(position).getId()+"/"+beanList.get(position).getName());
                idd = beanList.get(position).getId();
                name = beanList.get(position).getName();
                popupWindow.dismiss();
            }
        });
        popupWindow.setBackgroundDrawable(getResources().getDrawable(android.R.color.transparent));
        popupWindow.showAsDropDown(view);
        // 设置背景颜色变暗
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = 0.5f;
        getWindow().setAttributes(lp);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp = getWindow().getAttributes();
                lp.alpha = 1f;
                getWindow().setAttributes(lp);
            }
        });

    }

    private void showPop(View view)
    {
        PopDialog popDialog = new PopDialog(this, beanList, new PopDialog.PriorityListener() {
            @Override
            public void setActivityText(ItemBean ib) {
                tvc1.setText(ib.getId()+"/"+ib.getName());
                idd = ib.getId();
                name = ib.getName();
            }
        });
        popDialog.showAsDropDown(view);
    }

    @Override
    protected void onScanf(String code) {
        super.onScanf(code);
        if (deleteFlag)//删除条码
        {
            presenter.delCode(billCodeInfo,code);
        } else //增加条码
        {
            //showMsg("条码"+code+"长度:"+code.length());
            presenter.addCode(billCodeInfo,code);
        }
    }

    @Override
    public void showState(String str) {
        tvs.setText(str);
    }

    @Override
    public void showList(List<BillCodeInfo> list) {
        this.list = list;
        pa = new ProductAdapter(this, list);
        listview.setAdapter(pa);
        for (int i = 0; i <list.size() ; i++) {
            if(list.get(i).getFlag())
            {
                billCodeInfo = list.get(i);
                break;
            }
        }
    }

    @Override
    public void notifyList(List<BillCodeInfo> list) {
        this.list = list;
        pa.notifyDataSetChanged();
    }

    @Override
    public void showPlist(List<ItemBean> list) {
        beanList = list;
        if (beanList.size() > 0) {
            tvc1.setText(beanList.get(0).getId()+"/"+beanList.get(0).getName());
            idd = beanList.get(0).getId();
            name = beanList.get(0).getName();
        }
    }
}
